package com.rongji.dfish.demo.intro.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rongji.dfish.base.Page;
import com.rongji.dfish.demo.comm.service.CommService;
import com.rongji.dfish.demo.constant.DemoConstants;
import com.rongji.dfish.demo.intro.view.IntroduceView;
import com.rongji.dfish.demo.intro.view.IntroduceView.Menu;
import com.rongji.dfish.demo.pub.view.PubView;
import com.rongji.dfish.framework.FilterParam;
import com.rongji.dfish.framework.controller.BaseController;
import com.rongji.dfish.ui.command.CommandGroup;
import com.rongji.dfish.ui.command.JSCommand;
import com.rongji.dfish.ui.command.ReplaceCommand;
import com.rongji.dfish.ui.layout.View;
import com.rongji.dfish.ui.widget.Html;

@Controller
@RequestMapping("/intro")
public class IntroduceController extends BaseController {
	@Resource
	private CommService commService;
	
	@RequestMapping("/index")
	@ResponseBody
	public Object index(HttpServletRequest request) throws Exception {
		String anchor=request.getParameter("anchor");
		View view = IntroduceView.buildIndexView(anchor,DemoConstants.MOD_DOCUMENT);
		return view;
	}
	@RequestMapping("/download")
	@ResponseBody
	public Object download(HttpServletRequest request) throws Exception {
		String anchor=request.getParameter("anchor");
		View view = IntroduceView.buildDownloadView(anchor,DemoConstants.MOD_DOWNLOAD);
		return view;
	}
	/**
	 * 更新内容
	 * @param request
	 * @throws Exception
	 */
	@RequestMapping("/reloadText")
	@ResponseBody
	public Object reloadText(HttpServletRequest request) throws Exception {
		String textName = request.getParameter("textName");	
		Page page = getPage(request);
		View view = null;
		boolean isUpdates = "updates".equals(textName);
		if(isUpdates){
			view = IntroduceView.buildUpdatesView(page);
		} else{
			view = IntroduceView.buildInfoContentView(textName);
		}
		boolean replace="1".equals(request.getParameter("comm"));
		if(replace){
			CommandGroup command= new CommandGroup();
			command.add(new ReplaceCommand().setNode(view.findNodeById("ver1")));
			command.add(new ReplaceCommand().setNode(view.findNodeById("pageBar")));
			return command;
		} else{
			return view;
		}
	}

	@RequestMapping("/clickMenu")
	@ResponseBody
	public Object clickMenu(HttpServletRequest request) throws Exception {
		String menuId = request.getParameter("menuId");
		CommandGroup cg = new CommandGroup();
		Menu focusMenu = null;
		for (Menu menu : IntroduceView.getDownloadMenuData()) {
			if (menu.getMenuId().equals(menuId)) {
				focusMenu = menu;
			}
		}
		if (focusMenu != null) {
			View view = PubView.buildBodyView();
			Html mainTitle = (Html) view.findNodeById(PubView.MAIN_TITLE);
			mainTitle.setText(focusMenu.getMenuName());
			cg.add(new ReplaceCommand(mainTitle));
		}
		cg.add(new JSCommand("VM(this).find('mainContent').view('v_" + menuId + "');"));
		return cg;
	}
	
}
